package com.bitcoin.scheduler.task;

import com.bitcoin.scheduler.domain.Bcc;
import com.bitcoin.scheduler.domain.BccBasicDataCrawl;
import com.bitcoin.scheduler.domain.BccBasicInfo;
import com.bitcoin.scheduler.domain.BccBatch;
import com.bitcoin.scheduler.repository.BccBasicDataCrawlDao;
import com.bitcoin.scheduler.service.BccBasicInfoService;
import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 币基本信息爬虫数据清洗任务
 */
@Service
@Slf4j
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class BccBasicInfoTask implements Job {

    @Autowired
    private BccBasicInfoService basicInfoService;
    @Autowired
    private BccBasicDataCrawlDao basicDataCrawlDao;

    private final Mapper mapper = DozerBeanMapperBuilder.create()
        .withCustomConverter((source, target, aClass, aClass1) -> source != null ? source : target)
        .build();

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        List<BccBasicDataCrawl> crawlList = basicDataCrawlDao.findNewestCrawl();

        List<String> keyList = Lists.newArrayList();
        for (BccBasicDataCrawl crawl : crawlList) {
            BccBasicInfo basicInfo = basicInfoService.getDao().findByBccId(crawl.getBccId());
            if (basicInfo == null) {
                log.warn("批次详细不存在BccId");
                continue;
            }
            String sourceId = basicInfo.getId();
            log.info("同步更新BCC信息,before->BCC信息:{}", basicInfo);
            mapper.map(crawl, basicInfo);
            //还原ID
            basicInfo.setId(sourceId);
            basicInfo.setRecordState(Bcc.RecordState.PUBLISH);
            basicInfo.setOfficialSiteUrlCount(StringUtils.isEmpty(basicInfo.getOfficialSiteUrl()) ? 0 : 1);
            Date icoTime = basicInfo.getIcoEndTime();
            if (icoTime != null) {
                basicInfo.setIcoToNowDay((int) TimeUnit.DAYS.convert(new Date().getTime() - icoTime.getTime(), TimeUnit.MILLISECONDS));
            }
            else {
                basicInfo.setIcoToNowDay(180);
            }
            Date onlineTime = basicInfo.getOnlineTime();
            if (onlineTime != null) {
                basicInfo.setOnlineToNowDay((int) TimeUnit.DAYS.convert(new Date().getTime() - onlineTime.getTime(), TimeUnit.MILLISECONDS));
            }
            else {
                basicInfo.setOnlineToNowDay(180);
            }
            basicInfo = basicInfoService.getDao().save(basicInfo);
            log.info("同步更新BCC信息,after->BCC信息:{}", basicInfo);
            //用BCC ID作为KEY值
            keyList.add(basicInfo.getBccId());
        }
        //更新有关BCC的缓存
        updateCache(keyList);
    }

    //更新缓存
    private void updateCache(List<String> keyList) {
        log.info("同步更新BCC信息缓存,缓存KEY:{}", StringUtils.join(keyList, ","));
        for (String bccId : keyList) {
            basicInfoService.getDao().findAllBccId();
        }
    }
}
